package br.ucb.webresource;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

import br.ucb.bean.Aluno;
import br.ucb.bean.PlanoDeEnsino;
import br.ucb.bean.Turma;
import br.ucb.bean.TurmaShort;
import br.ucb.dao.AlunoDAO;
import br.ucb.dao.PlanoDeEnsinoDAO;
import br.ucb.dao.TurmaDAO;
import br.ucb.util.SimpleEntityManager;

import com.google.gson.Gson;

@Path("/turma")
public class TurmaResource implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@POST
	@Path("/getTurmas")
	@Produces("application/json")
	@Consumes("application/json")
	public List<TurmaShort> getTurmas( Aluno aluno){
		Aluno temp = new AlunoDAO(new SimpleEntityManager("sacop").getEntityManager()).buscar("codigo", aluno.getCodigo());
		List<Turma> turmas = new TurmaDAO(new SimpleEntityManager("sacop").getEntityManager()).buscarRelacionado("disciplina.curso.codigo", temp.getCurso().getCodigo());
		return buildTurmaShort(turmas);
		
	}
	
	@POST
	@Path("getTurmasGSON")
	@Produces("application/json")
	@Consumes("application/json")
	public String getTurmasGSON(Aluno aluno){
		Aluno temp = new AlunoDAO(new SimpleEntityManager("sacop").getEntityManager()).buscar("codigo", aluno.getCodigo());
		List<Turma> turmas = new TurmaDAO(new SimpleEntityManager("sacop").getEntityManager()).buscarRelacionado("disciplina.curso.codigo", temp.getCurso().getCodigo());
		return new Gson().toJson(buildTurmaShort(turmas));
	}

	@POST
	@Path("/getTurmasAluno")
	@Produces("application/json")
	@Consumes("application/json")
	public List<TurmaShort> getTurmasbyAluno( Aluno aluno){
		Aluno temp = new AlunoDAO(new SimpleEntityManager("sacop").getEntityManager()).buscar("codigo", aluno.getCodigo());
		return buildTurmaShort(temp.getTurmas());
		
	}
	
	@POST
	@Path("getTurmasAlunoGSON")
	@Produces("application/json")
	@Consumes("application/json")
	public String getTurmasbyAlunoGSON(Aluno aluno){
		Aluno temp = new AlunoDAO(new SimpleEntityManager("sacop").getEntityManager()).buscar("codigo", aluno.getCodigo());
		return new Gson().toJson(buildTurmaShort(temp.getTurmas()));
	}
	
	private List<TurmaShort> buildTurmaShort(List<Turma> turmas) {
		List<TurmaShort> turmasShort = new ArrayList<TurmaShort>();
		for (Turma turma : turmas) {
			turmasShort.add(new TurmaShort(turma.getCodigo(), turma.getDisciplina().getNome(), turma.getSenha()));
		}
		System.out.println(turmasShort);
		return turmasShort;
	}
	
	@POST
	@Path("/getformula")
	@Produces("application/json")
	@Consumes("application/json")
	public String getFormula(TurmaShort turmashort){
		Turma turma = new TurmaDAO(new SimpleEntityManager("sacop").getEntityManager()).getById(turmashort.getTurmaId());
		PlanoDeEnsino ps = new PlanoDeEnsinoDAO(new SimpleEntityManager("sacop").getEntityManager()).buscar("disciplina.codigo", turma.getDisciplina().getCodigo());
		return ps.getAvaliacao();
	}

}
